package com.iwhalecloud.bss.kite.cucc.service.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class LocalCollectionUtils extends org.springframework.util.CollectionUtils {

    public static List list(Object... values) {
        List list = new ArrayList();
        for (Object value : values) {
            list.add(value);
        }
        return list;
    }

    public static Object unique(List list, String errorMessage) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() > 1) {
            throw new RuntimeException(errorMessage);
        }
        else {
            return list.get(0);
        }
    }

    public static Object unique(List list) {
        return unique(list, "列表包含多个值!");
    }

    public static <T> List<T> safe(List<T> list) {
        if (list == null) {
            return Collections.emptyList();
        }
        return list;
    }

    /** 集合对象存在判断 add by xiaof 120223 */
    public static boolean contains(List list, Object object, Comparator comparator) {
        if (comparator == null) {
            safe(list).contains(object);
        }

        for (Object element : list) {
            if (comparator.compare(element, object) == 0) {
                return true;
            }
        }

        return false;
    }
}
